package com.fau.sd.servlet;

import java.io.IOException;
import java.sql.Types;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.fau.sd.timer.RemindTask;
import com.fau.sd.util.SystemConfig;
import com.fau.sd.util.UuidUtil;

/**
 * @desc 判断新建的话务任务是否需要当天运行
 * @author liy
 * @datetime 2015年1月5日,上午9:23:15
 */
@WebServlet("/inform")
public class InFormServlet extends HttpServlet {
       
	private static final long serialVersionUID = -8626177717135955210L;
	
	private JdbcTemplate jdbcTarget;
	

    public InFormServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cycle = request.getParameter("cycle");
		String reminderTime = request.getParameter("reminderTime");
		String operatorId = request.getParameter("operatorId");
		String content = request.getParameter("content");
		String eid = request.getParameter("eid");
		
		this.charge(cycle, reminderTime, operatorId, content);
		
		ServletContext servletContext = this.getServletContext();  
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        
        jdbcTarget = (JdbcTemplate) context.getBean("jdbcTarget");
        
        jdbcTarget.update("insert into medical_message values(?,?,?,?,?,?) "
        		, new Object[]{UuidUtil.getUuid(),content,0,eid,new Date(),operatorId}
        		, new int[]{Types.VARCHAR,Types.VARCHAR,Types.INTEGER,Types.VARCHAR,Types.DATE,Types.VARCHAR});
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	/**
	 * @desc 判断提醒是否今天就要执行
	 * @author liy
	 * @throws ParseException
	 * @datetime 2014年12月31日,下午5:51:58
	 */
	private void charge(String cycle,String reminderTime,String uid,String content) {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH) + 1;
		int date = c.get(Calendar.DATE);
		int week = c.get(Calendar.DAY_OF_WEEK) - 1;
		
		Date now = new Date();
		
		if(SystemConfig.WEEK_REMIND.equals(cycle)) {	//每周
			String[] time = reminderTime.split("\\|");
			String w = week+"";
			if(w.equals(time[0]) && this.getCurrDate(c,time[1]).after(now)){
				this.startTask(uid, content, this.getCurrDate(c,time[1]));
			}
		} else if(SystemConfig.DAY_REMIND.equals(cycle)) {	//每天
			if(this.getCurrDate(c,reminderTime).after(now)) {
				this.startTask(uid, content, this.getCurrDate(c,reminderTime));
			}
		} else if(SystemConfig.MONTH_REMIND.equals(cycle)) {	//每月
			String[] time = reminderTime.split("\\|");
			String d = date + "";
			if(d.equals(time[0]) && this.getCurrDate(c,time[1]).after(now)){
				this.startTask(uid, content, this.getCurrDate(c,time[1]));
			}
		} else if(SystemConfig.YEAR_REMIND.equals(cycle)) {	//每年
			String[] time = reminderTime.split("\\|");
			String m = month + "";
			String d = date + "";
			if(m.equals(time[0]) && d.equals(time[1]) && this.getCurrDate(c,time[3]).after(now)) {
				this.startTask(uid, content, this.getCurrDate(c,time[3]));
			}
		} else if(SystemConfig.ONCE_REMIND.equals(cycle)) {	//一次性
			String[] time = reminderTime.split("\\|");
			String today = DateFormatUtils.format(c, "yyyy-MM-dd");
			if(today.equals(time[0]) && this.getCurrDate(c,time[1]).after(now)) {
				this.startTask(uid, content, this.getCurrDate(c,time[1]));
			}
		}
	}
	
	/**
	 * @desc 获得当天的日期对象
	 * @param c 当天日期
	 * @param hm 时分
	 * @return Date
	 * @author liy
	 * @datetime 2015年1月4日,上午10:17:12
	 */
	private Date getCurrDate(Calendar c,String hm) {
		String curr = DateFormatUtils.format(c, "yyyy-MM-dd");
		curr = curr + " " + hm;
		Date date = null;
		try {
			date = DateUtils.parseDate(curr, "yyyy-MM-dd HH:mm");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * @desc 开始定时任务
	 * @param uid 提醒用户Id
	 * @param content 提醒用户内容
	 * @param date 提醒日期
	 * @author liy
	 * @datetime 2015年1月5日,上午11:15:52
	 */
	private void startTask(String uid,String content,Date date) {
		Timer timer = new Timer();
		timer.schedule(new RemindTask(uid, content), date);
	}

}
